<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>Aria: socketServerExample.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Aria
   &#160;<span id="projectnumber">2.9.1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('socketServerExample_8cpp-example.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">socketServerExample.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p><a class="el" href="classArSocket.html" title="socket communication wrapper ">ArSocket</a> example server programThis is an example to show how to use <a class="el" href="classArSocket.html" title="socket communication wrapper ">ArSocket</a>. a server. This is the server program. The client is socketClientExample.cpp</p>
<p>This program opens a server on port 7777. It waits for the client to connect and says hello to the client. It then waits for the client to say hello and then exits.</p>
<p>First run this server program. Then run the client program, socketClientExample, perhaps in a different terminal window. You should see the server accept the connection, send its greeting to the client, recieve a response from the client, and then the connection closes.</p>
<p>This server only accepts one client connection, and then it exits. In practice, most servers would loop, accepting many clients, and simultaneously handling curently open client connections. You can use threads to do that (see <a class="el" href="classArASyncTask.html" title="Asynchronous task (runs in its own thread) ">ArASyncTask</a>), but <a class="el" href="classArSocket.html" title="socket communication wrapper ">ArSocket</a> is not inherently thread-safe, you would also need to use an <a class="el" href="classArMutex.html" title="Cross-platform mutex wrapper class. ">ArMutex</a> object to protect <a class="el" href="classArSocket.html" title="socket communication wrapper ">ArSocket</a> calls.</p>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="classArSocket.html" title="socket communication wrapper ">ArSocket</a> </dd>
<dd>
socketClientExample.cpp</dd></dl>
<p>Also see the <a class="el" href="classArNetServer.html" title="Class for running a simple net server to send/recv commands via text. ">ArNetServer</a> to help manage a server, or the ArNetworking library for a more complete networking framework.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;Aria.h&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">  <span class="comment">// The string to send to the client. </span></div><div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span> *strToSend=<span class="stringliteral">&quot;Hello Client&quot;</span>;</div><div class="line">  <span class="comment">// The buffer in which to recieve the hello from the client</span></div><div class="line">  <span class="keywordtype">char</span> buff[100];</div><div class="line">  <span class="comment">// The size of the string the client sent</span></div><div class="line">  <span class="keywordtype">size_t</span> strSize;</div><div class="line"></div><div class="line">  <span class="comment">// Initialize Aria.  This is especially essential on Windows,</span></div><div class="line">  <span class="comment">// because it will initialize Windows&#39;s sockets sytem.</span></div><div class="line">  <a name="a0"></a><a class="code" href="classAria.html#ad99c16b5d947229d9f8e1c5b2d4cdd73">Aria::init</a>();</div><div class="line"></div><div class="line">  <span class="comment">// The socket objects: one for accepting new client connections,</span></div><div class="line">  <span class="comment">// and another for communicating with a client after it connects.</span></div><div class="line">  <a name="_a1"></a><a class="code" href="classArSocket.html">ArSocket</a> serverSock, clientSock;</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// Open the server socket</span></div><div class="line">  <span class="keywordflow">if</span> (serverSock.<a name="a2"></a><a class="code" href="classArSocket.html#a7a9bd59b92bd1018c8be0af39eac2dc8">open</a>(7777, ArSocket::TCP))</div><div class="line">    <a name="a3"></a><a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a4"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Opened the server port.&quot;</span>);</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">  {</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Failed to open the server port: %s.&quot;</span>,</div><div class="line">       serverSock.<a name="a5"></a><a class="code" href="classArSocket.html#a940299aab2087e9247dece7b7fe01a5c">getErrorStr</a>().c_str());</div><div class="line">    <a name="a6"></a><a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(-1);</div><div class="line">    <span class="keywordflow">return</span>(-1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">for</span>(<span class="keywordtype">int</span> clientNo = 0; <a name="a7"></a><a class="code" href="classAria.html#a573c386c42739be20fa97a99ae65f9ee">Aria::getRunning</a>(); ++clientNo)</div><div class="line">  {</div><div class="line"></div><div class="line">    <span class="comment">// Wait for a client to connect to us.</span></div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Waiting for a client to connect. Press CTRL-C to exit.&quot;</span>);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (serverSock.<a name="a8"></a><a class="code" href="classArSocket.html#a6647efb8d20f9523dad090a68b1e4e2c">accept</a>(&amp;clientSock))</div><div class="line">      <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Client %d has connected.&quot;</span>, clientNo);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">      <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a9"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;socketServerExample: Error in accepting a connection from the client: %s.&quot;</span>,</div><div class="line">         serverSock.<a class="code" href="classArSocket.html#a940299aab2087e9247dece7b7fe01a5c">getErrorStr</a>().c_str());</div><div class="line"></div><div class="line">    <span class="comment">// Send the string &#39;Hello Client&#39; to the client. write() should</span></div><div class="line">    <span class="comment">// return the same number of bytes that we told it to write. Otherwise,</span></div><div class="line">    <span class="comment">// its an error condition.</span></div><div class="line">    <span class="keywordflow">if</span> (clientSock.<a name="a10"></a><a class="code" href="classArSocket.html#af2cfbd564eec7020283f5a6fe41f558f">write</a>(strToSend, strlen(strToSend)) == (int) strlen(strToSend))</div><div class="line">      <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Said hello to the client.&quot;</span>);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">      <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Error in sending hello string to the client.&quot;</span>);</div><div class="line">      <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(-1);</div><div class="line">      <span class="keywordflow">return</span>(-1);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Read data from the client. read() will block until data is</span></div><div class="line">    <span class="comment">// received. </span></div><div class="line">    strSize=clientSock.<a name="a11"></a><a class="code" href="classArSocket.html#a6cf324be0075d3469a70d01d4740e23c">read</a>(buff, <span class="keyword">sizeof</span>(buff));</div><div class="line"></div><div class="line">    <span class="comment">// If the amount read is 0 or less, its an error condition.</span></div><div class="line">    <span class="keywordflow">if</span> (strSize &gt; 0)</div><div class="line">    {</div><div class="line">      <span class="comment">// Terminate the string with a NULL character.</span></div><div class="line">      buff[strSize]=<span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">      <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Client said: %s.&quot;</span>, buff);</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">      <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Error in waiting/reading the hello from the client.&quot;</span>);</div><div class="line">      <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(-1);</div><div class="line">      <span class="keywordflow">return</span>(-1);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Now lets close the connection to the client</span></div><div class="line">    clientSock.<a name="a12"></a><a class="code" href="classArSocket.html#a89ab9a4e404526343ccf6ba3b6644c58">close</a>();</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Socket to client closed.&quot;</span>);</div><div class="line">    </div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// And lets close the server port</span></div><div class="line">  serverSock.<a class="code" href="classArSocket.html#a89ab9a4e404526343ccf6ba3b6644c58">close</a>();</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketServerExample: Server socket closed.&quot;</span>);</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// Uninitialize Aria and exit the program</span></div><div class="line">  <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(0);</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span>(0);</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>
